// pages/batchTiedCard/batchTiedCard.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeList:[
      // '12345',
      // '22345',
      // '32345',
      // '42345',
      // '52345',
      // '62345',
      // '72345',
      // '82345',
      // '92345',
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  scanCode(){
    var that = this
    wx.scanCode({
      success(res){
        let data = res.result
        let arr = data.split('/')
        let code = parseInt(arr[arr.length-1])
        if(that.data.codeList.indexOf(code) == -1){

        app.executeRequest(function (header) {
          wx.request({
            url: app.BASE_URL + '/clk/isIcCardBind',
            header,
            data:{
              cardNumber:code
            },
            success: (res) => {
              console.log(res)
              if(res.data.result == 0){
                  that.setData({
                    codeList:that.data.codeList.concat(code)
                  })
                  console.log(that.data.codeList)
                
              }else{
                wx.showModal({
                  title:'温馨提示',
                  content: res.data.description,
                  showCancel:false
                })
              }
    
            },
            fail:(err)=>{
              console.log(err)
            }
          })
    
        })
      }
      }
    })
  },
  submit(){
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    app.executeRequest(function (header) {
      wx.request({
        url: app.BASE_URL + '/clk/bindCards',
        header,
        data:{
          cardNumberList:that.data.codeList.join(",")
        },
        success: (res) => {
          getApp().hideLoading()
          console.log(res)
          if(res.data.result == 0){
            wx.showToast({
              title: '绑定成功',
            })
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1,
              })
            },2000)
          }else{
            wx.showModal({
              title:'温馨提示',
              content: res.data.description,
              showCancel:false
            })
          }

        },
        fail:(err)=>{
          getApp().hideLoading()
          console.log(err)
        }
      })

    })
  },
  deleteThis(e){
    var that = this
    that.data.codeList.splice(e.currentTarget.dataset.index,1)
    that.setData({
      codeList:that.data.codeList
    })
    console.log(that.data.codeList)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})